package com.itheima.demo;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 实现  document 的 增删改查
 * @作者 itcast
 * @创建日期 2021/4/2 8:54
 **/
public class EsDemo02 {

    RestHighLevelClient client;

    /**
     * 初始化es 客户端
     */
    @Before
    public void init(){
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.200.130", 9200, "http")));


    }

    /**
     * 添加文档 或 修改文档
     */
    @Test
    public void addDocument() throws IOException {
        User user = new User(1L,"鲁班",30,"1","鲁班在河道唱着歌");
        // 1. 创建 添加文档的请求  IndexRequest
        IndexRequest request = new IndexRequest("user");
        // 设置文档ID
        request.id(user.getId()+"");
        // 设置文档内容
        // 1. 参数: 文档的json字符串   2. 类型: json类型
        String userJson = JSON.toJSONString(user);
        request.source(userJson,XContentType.JSON);
        // 2. 使用客户端执行 client.index(request);
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        // 3. 解析响应结果
        System.out.println(response.getResult());
    }

    @Test
    public void findDocumentById() throws IOException {
//        // 1. 创建一个对应的请求对象 GetRequest
        GetRequest request = new GetRequest("user","1");
//        设置文档ID
//// 2. 客户端执行get查看文档
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
//// 3. 解析响应结果
//        获取source字段内容 (json字符串)
        String userJson = response.getSourceAsString();
//        反序列化成json对象
        User user = JSON.parseObject(userJson,User.class);

        System.out.println(user);
    }

    /**
     * 上面的Index方法 即可以添加文档 又可以修改文档
     * 如果你只想改某个字段  可以使用update方法
     * @throws IOException
     */
    @Test
    public void updateDocument() throws IOException {
        // 1. 创建 添加文档的请求  IndexRequest
        UpdateRequest request = new UpdateRequest("user","1");
        request.doc("gender","0");
        // 2. 使用客户端执行 client.index(request);
        UpdateResponse update = client.update(request, RequestOptions.DEFAULT);
        // 3. 解析响应结果
        System.out.println(update.getResult());
    }


    @Test
    public void deleteDocumentById() throws IOException {
        // 1. 创建
        DeleteRequest request = new DeleteRequest("user","1");
        // 2. 客户端执行请求
        DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);
        // 3. 解析结果
        System.out.println(delete.getResult());
    }

    @Test
    public void bulkAddDocument() throws IOException {
        // List<User> 要存的数据
        List<User> userList = Arrays.asList(
                new User(1L, "Rose", 18, "1", "Rose同学在传智播客学表演"),
                new User(2L, "Jack", 38, "1", "Jack同学在黑马学JavaEE"),
                new User(3L, "小红", 23, "0", "小红同学在传智播客学唱歌"),
                new User(4L, "小明", 20, "1", "小明同学在黑马学JavaSE"),
                new User(5L, "达摩", 33, "1", "达摩和尚在达摩院学唱歌"),
                new User(6L, "鲁班", 24, "1", "鲁班同学走在乡间小路上"),
                new User(7L, "孙尚香", 26, "0", "孙尚香同学想带阿斗回东吴"),
                new User(8L, "李白", 27, "1", "李白同学在山顶喝着酒唱着歌"),
                new User(9L, "甄姬", 28, "0", "甄姬同学弹奏一曲东风破"),
                new User(10L, "虞姬", 27, "0", "虞姬同学在和项羽谈情说爱")
        );
        // 1. 创建批处理请求  BulkRequest
        BulkRequest request = new BulkRequest("user");
        //        1.1 循环遍历集合数据
        for (User user : userList) {
            //        1.2 每一次遍历封装添加文档请求  IndexRequest
            IndexRequest indexRequest = new IndexRequest();
            indexRequest.id(user.getId() + "");
            indexRequest.source(JSON.toJSONString(user),XContentType.JSON);
            //        1.3 将IndexRequest加入到BulkRequest中(加入到批处理中)
            request.add(indexRequest);
        }
        // 2. 客户端执行bulk();
        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        // 3. 解析结果
        System.out.println(response.status());
    }


    /**
     * 关闭es 客户端
     */
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
